package com.example.demo.util;

import com.example.demo.entity.admin_logs;
import com.example.demo.mapper.admin_logs_mapper;
import org.apache.ibatis.session.SqlSession;

public class LogUtil {

    //    getAlllog查询全部日志记录
    public static Object getAlllog() throws Exception {
        SqlSession sqlSession = null;
        try {
            sqlSession = SqlSessionUtil.GetSession();
            admin_logs_mapper mapper = sqlSession.getMapper(admin_logs_mapper.class);
            return mapper.selectAllLogs();
        } catch (Exception e) {
            System.err.println("严重错误！！！Error logging: " + e.getMessage()); // 更详细的错误信息
            if (sqlSession != null) {
                sqlSession.rollback();
            }
            return null;
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }

    //    error发生错误事件，但仍不影响系统的继续运行
    public static boolean error(String Type, String Message) {
        SqlSession session = null;
        try {
            session = SqlSessionUtil.GetSession();
            admin_logs_mapper mapper = session.getMapper(admin_logs_mapper.class);

            admin_logs admin_logs = new admin_logs();
            admin_logs.setLog_Level("error");
            admin_logs.setLog_type(Type);
            admin_logs.setLog_Message(Message);

            if (mapper.insertLog(admin_logs) >= 1) {
                session.commit();
                return true;
            } else {
                session.rollback();
                return false;
            }
        } catch (Exception e) {
            System.out.println("严重错误！！！error logging:" + e.getMessage());
            if (session != null) {
                session.rollback();
            }
            return false;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    //    WARN警告，即潜在的错误情形
    public static boolean warn(String Type, String Message) {
        SqlSession session = null;
        try {
            session = SqlSessionUtil.GetSession();
            admin_logs_mapper mapper = session.getMapper(admin_logs_mapper.class);

            admin_logs admin_logs = new admin_logs();
            admin_logs.setLog_Level("warn");
            admin_logs.setLog_type(Type);
            admin_logs.setLog_Message(Message);

            if (mapper.insertLog(admin_logs) >= 1) {
                session.commit();
                return true;
            } else {
                session.rollback();
                return false;
            }
        } catch (Exception e) {
            System.out.println("严重错误！！！Warn logging:" + e.getMessage());
            if (session != null) {
                session.rollback();
            }
            return false;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    //    INFO一般和在粗粒度级别上，强调应用程序的运行全程
    public static boolean INFO(String Type, String Message) {
        SqlSession session = null;
        try {
            session = SqlSessionUtil.GetSession();
            admin_logs_mapper mapper = session.getMapper(admin_logs_mapper.class);

            admin_logs admin_logs = new admin_logs();
            admin_logs.setLog_Level("INFO");
            admin_logs.setLog_type(Type);
            admin_logs.setLog_Message(Message);

            if (mapper.insertLog(admin_logs) >= 1) {
                session.commit();
                return true;
            } else {
                session.rollback();
                return false;
            }
        } catch (Exception e) {
            System.out.println("严重错误！！！INFO logging:" + e.getMessage());
            if (session != null) {
                session.rollback();
            }
            return false;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    //    DEBUG一般用于细粒度级别上，对调试应用程序非常有帮助
    public static boolean DEBUG(String Type, String Message) {
        SqlSession session = null;
        try {
            session = SqlSessionUtil.GetSession();
            admin_logs_mapper mapper = session.getMapper(admin_logs_mapper.class);

            admin_logs admin_logs = new admin_logs();
            admin_logs.setLog_Level("DEBUG");
            admin_logs.setLog_type(Type);
            admin_logs.setLog_Message(Message);

            if (mapper.insertLog(admin_logs) >= 1) {
                session.commit();
                return true;
            } else {
                session.rollback();
                return false;
            }
        } catch (Exception e) {
            System.out.println("严重错误！！！DEBUG logging:" + e.getMessage());
            if (session != null) {
                session.rollback();
            }
            return false;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }
}
